/*
 * TimeUtils.cpp
 */

#include <sstream>
#include <time.h>
#include "TimeUtils.h"

TimeUtils::TimeUtils() {
	creationTime = time(NULL);
}

TimeUtils::~TimeUtils() {}

const std::string TimeUtils::getDayName(int dayNumber) {
	switch (dayNumber) {
	case 0:	return std::string("Sun");
	case 1:	return std::string("Mon");
	case 2:	return std::string("Tue");
	case 3:	return std::string("Wed");
	case 4:	return std::string("Thu");
	case 5:	return std::string("Fri");
	case 6:	return std::string("Sat");
	default: return std::string();
	}
}

const std::string TimeUtils::getMonthName(int monthNumber) {
	switch (monthNumber) {
	case 1:	return std::string("Jan");
	case 2:	return std::string("Feb");
	case 3:	return std::string("Mar");
	case 4:	return std::string("Apr");
	case 5:	return std::string("May");
	case 6:	return std::string("Jun");
	case 7:	return std::string("Jul");
	case 8:	return std::string("Ago");
	case 9:	return std::string("Sep");
	case 10: return std::string("Oct");
	case 11: return std::string("Nov");
	case 12: return std::string("Dec");
	default: return std::string();
	}
}

const std::string TimeUtils::formatNumber(int number, size_t longitud) {
	std::stringstream ss;
	ss << number;
	std::string salida(ss.str());
	size_t tamCad = salida.length();
	if(tamCad < longitud) {
		salida.insert(0, longitud - tamCad, '0');
	}
	return salida;
}

const std::string TimeUtils::rfc1123TimeStamp() {
	std::stringstream ss;
	time_t gmtTime = time(NULL);
    struct tm* pTm = gmtime(&gmtTime);

    ss << TimeUtils::getDayName(pTm->tm_wday) << ", ";
    ss << TimeUtils::formatNumber(pTm->tm_mday) << " ";
    ss << TimeUtils::getMonthName(pTm->tm_mon + 1) << " ";
    ss << (pTm->tm_year + 1900) << " ";
    ss << TimeUtils::formatNumber(pTm->tm_hour) << ":";
    ss << TimeUtils::formatNumber(pTm->tm_min) << ":";
    ss << TimeUtils::formatNumber(pTm->tm_sec) << " GMT";
    return ss.str();
}

int TimeUtils::getElapsedTimed() const {
	return (int)(difftime(time(NULL), creationTime));
}
